import 'package:flutter/material.dart';

/// 一个通用的标签组件（Tag）
/// - 通过工厂方法按状态生成内置的前景/背景色
/// - 也可以直接传入自定义颜色
class Tag extends StatelessWidget {
  /// 标签文本
  final String text;

  /// 文字颜色
  final Color foregroundColor;

  /// 背景色
  final Color backgroundColor;

  /// 圆角
  final BorderRadius borderRadius;

  /// 内边距
  final EdgeInsetsGeometry padding;

  const Tag._internal({
    Key? key,
    required this.text,
    required this.foregroundColor,
    required this.backgroundColor,
    this.borderRadius = const BorderRadius.all(Radius.circular(4)),
    this.padding = const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
  }) : super(key: key);

  /// 工厂方法：根据状态生成不同配色
  factory Tag.byStatus(
    String text,
    TagStatus status, {
    BorderRadius borderRadius = const BorderRadius.all(Radius.circular(4)),
    EdgeInsetsGeometry padding = const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
  }) {
    final _TagPalette palette = _paletteOf(status);
    return Tag._internal(
      text: text,
      foregroundColor: palette.fg,
      backgroundColor: palette.bg,
      borderRadius: borderRadius,
      padding: padding,
    );
  }

  /// 工厂方法：仅传入文案，内部根据常见关键字自动映射状态
  /// 例如："技术/开发" -> primary，"趋势" -> info，"设计" -> success，"优化" -> warning，"架构" -> danger，其他 -> neutral
  factory Tag.auto(
    String text, {
    BorderRadius borderRadius = const BorderRadius.all(Radius.circular(4)),
    EdgeInsetsGeometry padding = const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
  }) {
    final status = mapLabelToStatus(text);
    return Tag.byStatus(
      text,
      status,
      borderRadius: borderRadius,
      padding: padding,
    );
  }

  /// 直接自定义颜色的构造（可绕过状态映射）
  factory Tag.custom(
    String text, {
    required Color foreground,
    required Color background,
    BorderRadius borderRadius = const BorderRadius.all(Radius.circular(4)),
    EdgeInsetsGeometry padding = const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
  }) {
    return Tag._internal(
      text: text,
      foregroundColor: foreground,
      backgroundColor: background,
      borderRadius: borderRadius,
      padding: padding,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding,
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: borderRadius,
      ),
      child: Text(
        text,
        style: TextStyle(
          fontSize: 10,
          fontWeight: FontWeight.w500,
          color: foregroundColor,
        ),
      ),
    );
  }
}

/// 标签状态枚举
enum TagStatus { primary, info, success, warning, danger, neutral }

/// 根据文案自动映射状态的辅助方法
TagStatus mapLabelToStatus(String label) {
  final text = label.toLowerCase();
  if (text.contains('技') || text.contains('dev') || text.contains('开发') || text.contains('flutter')) {
    return TagStatus.primary;
  }
  if (text.contains('趋势') || text.contains('trend') || text.contains('新闻') || text.contains('news')) {
    return TagStatus.info;
  }
  if (text.contains('设计') || text.contains('ui') || text.contains('ux')) {
    return TagStatus.success;
  }
  if (text.contains('优化') || text.contains('performance') || text.contains('perf')) {
    return TagStatus.warning;
  }
  if (text.contains('架构') || text.contains('安全') || text.contains('danger')) {
    return TagStatus.danger;
  }
  return TagStatus.neutral;
}

/// 一个简单的调色板结构
class _TagPalette {
  final Color fg;
  final Color bg;
  const _TagPalette(this.fg, this.bg);
}

/// 各状态对应的前景/背景色
_TagPalette _paletteOf(TagStatus status) {
  switch (status) {
    case TagStatus.primary:
      return const _TagPalette(Color(0xFF2962FF), Color(0x1A2962FF)); // 蓝 / 10% 背景
    case TagStatus.info:
      return const _TagPalette(Color(0xFF0288D1), Color(0x1A0288D1)); // 青蓝
    case TagStatus.success:
      return const _TagPalette(Color(0xFF2E7D32), Color(0x1A2E7D32)); // 绿
    case TagStatus.warning:
      return const _TagPalette(Color(0xFFF57F17), Color(0x1AF57F17)); // 橙
    case TagStatus.danger:
      return const _TagPalette(Color(0xFFC62828), Color(0x1AC62828)); // 红
    case TagStatus.neutral:
      return const _TagPalette(Color(0xFF616161), Color(0x1A616161)); // 灰
  }
}
